﻿using Dapper;
using System.Collections.Generic;
using System.Linq;
using Tauren.Business.Entity.Output;
using Tauren.Business.Infrastructure.Globals;
using Model = Tauren.Business.Entity.Model;
using Dapper.Contrib.Extensions;

namespace Tauren.Business.DLL.Menus
{
    /// <summary>
    /// 菜单信息
    /// </summary>
    public class MenuBusiness : Base.BaseDLL, IMenuBusiness
    {
        public MenuBusiness(IConnectionDLLBase _dLLBase) : base(_dLLBase)
        {
        }

        public bool Delete(object MenuId)
        {
            bool b = dbConnection.Execute("DELETE FROM menus WHERE MenuId=@MenuId", new Entity.Model.Menus() { MenuId = (long)MenuId }) >= 0;
            dbConnection.Dispose();
            return b;
        }


        /// <summary>
        /// 根据权限获取数据
        /// </summary>
        /// <returns></returns>
        public List<Entity.Model.Menus> GetMenus()
        {
            List<Entity.Model.Menus> menus = new List<Entity.Model.Menus>();

            string sql = $"SELECT d.* FROM rolepersoninfo a LEFT JOIN roleinfo b ON a.roleid=b.autoid LEFT JOIN menus_roles c on c.roleid=b.autoid LEFT JOIN menus d on d.menuid=c.menuid WHERE a.empid={Global.EmpId} AND b.`enable`=1 AND d.`status`=1 ORDER BY c.sequence ASC";
            if (Global.IsAdmin)
            {
                sql = "select * from menus m  order by m.`sequence` ";
            }
            menus = dbConnection.Query<Entity.Model.Menus>(sql).ToList();
            dbConnection.Dispose();
            return menus;
        }
        /// <summary>
        /// 获取单个菜单--根据一个编码
        /// </summary>
        /// <param name="Code"></param>
        /// <returns></returns>
        public Model.Menus GetByCode(string Code)
        {
            var list = dbConnection.QueryFirstOrDefault<Model.Menus>("SELECT * FROM Menus WHERE Code=@Code", new Model.Menus() { Code = Code });
            dbConnection.Dispose();
            return list;

        }

        /// <summary>
        /// 根据父级编号获取数据
        /// </summary>
        /// <param name="ParentCode"></param>
        /// <returns></returns>
        public double GetSequenceByParentCode(string ParentCode)
        {
            string Sql = "";
            string ParentSql = "";
            if (string.IsNullOrEmpty(ParentCode))
            {
                Sql = "select max(`sequence`) as seq  from menus m where m.parentcode is null or  m.parentcode ='' ;";
                ParentSql = "select `sequence` as seq  from menus m where m.code is null or  m.code ='';";
            }
            else
            {
                Sql = $"select max(`sequence`)  from menus m where m.parentcode='{ParentCode}' ;";
                ParentSql = $"select `sequence`   from menus m where m.code ='{ParentCode}';";
            }
            var Result = dbConnection.ExecuteScalar<object>(Sql);
            var parentResult = dbConnection.ExecuteScalar<object>(ParentSql);
            dbConnection.Dispose();
            if (parentResult == null)
            {
                return Result == null ? 0 : (double)Result;
            }
            else
            {
                return Result == null ? (double)parentResult * 100 : (double)Result;
            }

        }

        /// <summary>
        /// 根据父级编码查询子集编码
        /// </summary>
        /// <returns></returns>
        public List<Model.Menus> GetMenusByParentCode(object id)
        {
            var menus = dbConnection.Query<Entity.Model.Menus>("select * from menus m where m.parentcode in ( select code from menus b where b.menuid =@MenuId)  ;", new Model.Menus { MenuId = (long)id }).ToList();
            dbConnection.Dispose();
            return menus;
        }

        public List<ExtMenus_Roles> Roles(long MenuId)
        {
            var menus = dbConnection.Query<ExtMenus_Roles>($"SELECT a.menuid,a.roleid,b.`code` as rolecode,c.`value` as roleName FROM menus_roles a left JOIN roleinfo b on a.roleid=b.autoid LEFT JOIN roleres c on c.roleid=b.autoid AND c.`language`='{Global.Language}' WHERE a.menuid=@MenuId;", new Model.Menus { MenuId = MenuId }).ToList();
            dbConnection.Dispose();
            return menus;
        }

        public bool AddRoles(List<Model.Menus_Roles> Roles, long menuId)
        { 
            bool b = dbConnection.Execute("DELETE FROM menus_roles WHERE menuid=@MenuId", new Entity.Model.Menus_Roles() { MenuId = menuId }) >= 0;
            if (b&&Roles.Any())
            {
                b = dbConnection.Insert(Roles) > 0;
            }
            dbConnection.Dispose();
            return b;
        }
    }
}
